//
//  DingyueController.m
//  网易新闻
//
//  Created by dllo on 15/10/5.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "DingyueController.h"
#import "MJRefresh.h"
#import "DingyueCell.h"
#import "AFTool.h"
#import "DingyueModel.h"
#import "DetailsController.h"
#import "DingyueDetailsController.h"
#import "Settings.h"
@interface DingyueController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSArray *modArr;
@property(nonatomic, assign)NSInteger cellheight;
@property(nonatomic, retain)DetailsController *result;

@end

@implementation DingyueController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.cellheight = 44;
        [self createTableView];
    }
    return self;
}

- (void)viewWillLayoutSubviews{
    [self.tableView.header beginRefreshing];
    self.tableView.contentOffset = CGPointMake(0, 0);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    [self notificationHandle];
}

- (void)createTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 565) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView release];
    [self.tableView registerClass:[DingyueCell class] forCellReuseIdentifier:@"DingyueCell"];
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self handelDataWithURL:@"http://c.3g.163.com/nc/topicset/subscribe/recommend.html"];
        [self.tableView.header endRefreshing];
    }];
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self handelDataWithURL:@"http://c.3g.163.com/nc/topicset/subscribe/recommend.html"];
        [self.tableView.footer endRefreshing];
    }];
}

- (void)handelDataWithURL:(NSString *)url{
    [AFTool GETWithURL:url body:nil httpHead:nil responseStyle:JSON success:^(id result){
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:result];
        self.modArr = [[NSArray alloc]initWithArray:[tempDic objectForKey:@"recommendlist"]];
        [[Settings shareSettings]writeToDBWithDic:tempDic WithName:url];
        [tempDic release];
        [self.tableView reloadData];
    } fail:^(NSError *error) {
        NSDictionary *tempDic = [[NSDictionary alloc]initWithDictionary:[[Settings shareSettings]dicSelectFromDB:url]];
        if (![tempDic isEqual:nil]) {
            self.modArr = [[NSArray alloc]initWithArray:[tempDic objectForKey:@"recommendlist"]];
            [tempDic release];
            [self.tableView reloadData];
        }
        NSLog(@"404(DingyueController)");
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DingyueModel *mod = [DingyueModel modWithDic:self.modArr[indexPath.row]];
    DingyueCell *cell = [[DingyueCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"DingyueCell"];
    cell.mod = mod;
    cell.buttonClick = ^(NSString *viewid, NSString *type, NSString *desc, NSString *title){
        if ([type isEqualToString:@"tid"]) {
            DingyueDetailsController *details = [[DingyueDetailsController alloc]init];
            details.tid = viewid;
            details.desc = desc;
            details.imgname = @"DingyueBack";
            details.title = title;
            [self.navigationController pushViewController:details animated:YES];
            [details release];
        }else{
            DetailsController *details = [[DetailsController alloc]init];
            details.docid = viewid;
            [self.navigationController pushViewController:details animated:YES];
            [details release];
        }
    };
    self.cellheight = cell.height;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.cellheight;
}

- (void)notificationHandle{
    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        self.view.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.tableView.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    }];
}

- (void)dealloc{
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    [_modArr release];
    [_tableView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
